package com.migu.contacts.main;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.Key;
import com.frank.libandroid.util.RadioGroupUtil;
import com.migu.contacts.bean.BirthdayTip;
import com.migu.contacts.bean.ContactBean;
import com.migu.contacts.bean.GiftBean;
import com.migu.contacts.bean.GiftTip;
import com.migu.contacts.databinding.ActivityBirthdayBinding;
import com.migu.contacts.util.PopUtil;
import com.migu.contacts.widget.Callback;
import com.migu.contacts.widget.Label2;

public class BirthdayActivity extends BaseActivity<ActivityBirthdayBinding> {

    ContactBean contactBean = DataStore.get(ContactEditActivity.CONTACT_BEAN_KEY);
    BirthdayTip birthdayTip = null;

    @Override
    protected void initData() {
        mBinding.setContactBean(contactBean);
        if (contactBean.birthdayContactsTip == null) {
            contactBean.birthdayContactsTip = new BirthdayTip();
        }
        birthdayTip = contactBean.birthdayContactsTip;
        mBinding.setBean(birthdayTip);
        int checkIndex = 0;
        if (birthdayTip.birthdayType == 2) {
            checkIndex = 1;
        }
        RadioGroupUtil.checkIndex(mBinding.radioGroup, checkIndex);
        String s1 = contactBean.getBirthdayMothDay(checkIndex);
        if (!TextUtils.isEmpty(birthdayTip.tipTime)) {
            s1 = birthdayTip.tipTime;
        } else {
            birthdayTip.tipTime = s1;
        }
        Label2.LabelBean labelBean = new Label2.LabelBean("提醒日期", s1, 0, true);
        mBinding.birthdayLabel.setBean(labelBean);
        mBinding.birthdayLabel.mBinding.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDateTimePicker(labelBean);
            }
        });
        if (birthdayTip.giftTip == null) {
            birthdayTip.giftTip = new GiftTip();
        }
        String s2 = contactBean.getBirthdayMothDay(checkIndex);
        if (!TextUtils.isEmpty(birthdayTip.giftTip.tipTime)) {
            s2 = birthdayTip.giftTip.tipTime;
        } else {
            birthdayTip.giftTip.tipTime = s2;
        }
        Label2.LabelBean labelBean1 = new Label2.LabelBean("提醒日期", s2, 0, true);
        mBinding.giftLabel.setBean(labelBean1);
        mBinding.giftLabel.mBinding.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showGiftDateTimePicker(labelBean1);
            }
        });
        RadioGroupUtil.setOnCheckIndexChange(mBinding.radioGroup, new RadioGroupUtil.OnCheckIndexChange() {
            @Override
            public void onCheckIndexChange(int index) {
                Log.d(TAG, "onCheckIndexChange() called with: index = [" + index + "]");
                labelBean1.right = contactBean.getBirthdayMothDay(index);
                labelBean1.notifyChange();
                labelBean.right = contactBean.getBirthdayMothDay(index);
                labelBean.notifyChange();
                birthdayTip.giftTip.tipTime = labelBean1.right;
                birthdayTip.tipTime = labelBean1.right;
                birthdayTip.birthdayType = index + 1;
            }
        });
        mBinding.content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String string = s.toString();
                Log.i(TAG, "afterTextChanged: " + string);
            }
        });
    }

    private void showGiftDateTimePicker(Label2.LabelBean labelBean) {
        PopUtil.showMonthDayPicker(mActivity, labelBean.right, new Callback<String>() {
            @Override
            public void onComplete(String target) {
                labelBean.right = target;
                labelBean.notifyChange();
                birthdayTip.giftTip.tipTime = target;
            }
        });
    }

    private void showDateTimePicker(Label2.LabelBean labelBean) {
        PopUtil.showMonthDayPicker(mActivity, labelBean.right, new Callback<String>() {
            @Override
            public void onComplete(String target) {
                labelBean.right = target;
                labelBean.notifyChange();
                birthdayTip.tipTime = target;
            }
        });
    }

    @Override
    protected void initView() {
        RadioGroupUtil.checkIndex(mBinding.radioGroup, 0);
    }

    public void save(View view) {
        DataStore.put(KEY, birthdayTip);
        setResult(RESULT_OK);
        finish();
    }

    public static final Key<BirthdayTip> KEY = new Key<BirthdayTip>();

    public void goWords(View view) {
        start(BirthdayWordsActivity.class);
    }

    public void goGift(View view) {
        startActivityForResult(GiftSelectActivity.class, new Runnable() {
            @Override
            public void run() {
                GiftBean giftBean = DataStore.get(GiftSelectActivity.KEY);
                if (giftBean == null) {
                    return;
                }
                BirthdayTip birthdayTip = mBinding.getBean();
                if (birthdayTip.giftTip == null) {
                    birthdayTip.giftTip = new GiftTip();
                }
                birthdayTip.giftTip.content = giftBean.name;
            }
        });
    }
}